package downloads;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
/**
 * Download Category is used to structure downloads on the downloadpage.
 * It consistent of a variable number of downloads.
 * @author Thorsten Ludwig
 */

@Entity
public class DownloadCategory implements Serializable {
    
    private String name;
    @OneToMany(cascade=CascadeType.ALL)
    private List<Download> downloads;
    @Id
    @GeneratedValue
    private Long id;

    public DownloadCategory() {
    }
    
    public DownloadCategory(String name) {
        this.name=name;
        this.downloads = new ArrayList<Download>();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public void addDownload(Download d) {
        this.downloads.add(d);
    }
    
    public void removeDownload(Download d) {
        this.downloads.remove(d);
    }

    public List<Download> getDownloads() {
        return downloads;
    }
    
    public void moveDownloadUp(Download d) {
        int pos = Math.max(this.downloads.indexOf(d) - 1, 0);
        this.downloads.remove(d);
        this.downloads.add(pos, d);
    }
    
    public void moveDownloadDown(Download d) {
        int pos = Math.min(this.downloads.indexOf(d) + 1, this.downloads.size()-1);
        this.downloads.remove(d);
        this.downloads.add(pos, d);
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setDownloads(List<Download> downloads) {
        this.downloads = downloads;
    }
}
